/*
 * Copyright 2007 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.cal;

import java.util.*;
import java.text.*;

public class CalendarDate 
{
    private Date _date = null;
    private Vector _events = new Vector();

    public CalendarDate ( Date date )
    {
        _date = date;
    }

    public void setEvents ( Vector v )
    {
        _events = v;

        //this will order the collection by time
        Collections.sort( _events );
    }

    public Vector getEvents ()
    {
        return _events;
    }

    public CalendarEvent getEvent ( int id )
    {
        return (CalendarEvent) _events.elementAt( id );
    }

    public CalendarEvent removeEvent ( int id )
    {
        //shouldn't need to resort
        return (CalendarEvent) _events.remove( id );
    }

    public String toString ()
    {
        SimpleDateFormat shortFormat = new SimpleDateFormat( "yyyy-MM-dd" );
        SimpleDateFormat longFormat = new SimpleDateFormat( "EEEE MMMM d, yyyy" );
        SimpleDateFormat monthFormat = new SimpleDateFormat( "M" );
        SimpleDateFormat yearFormat = new SimpleDateFormat( "yyyy" );
        SimpleDateFormat dateFormat = new SimpleDateFormat( "d" );
        SimpleDateFormat diwFormat = new SimpleDateFormat( "EEEE" );

        StringBuffer buf = new StringBuffer();
        buf.append( "<calendarDate id='" ).append( shortFormat.format( _date ) ).append( "'" );
        buf.append( " month='" ).append( monthFormat.format( _date ) ).append( "'" );
        buf.append( " date='" ).append( dateFormat.format( _date ) ).append( "'" );
        buf.append( " year='" ).append( yearFormat.format( _date ) ).append( "'" );
        buf.append( " dayInWeek='" );
        if ( shortFormat.format( new Date() ).equals( shortFormat.format( _date ) ) )
        {
            buf.append( "Today" );
        }
        else
        {
            buf.append( diwFormat.format( _date ) );
        }
        buf.append( "'" );
        buf.append( " name='" ).append( longFormat.format( _date ) ).append( "'" );

        if ( _events.size() > 0 )
        {
            buf.append( ">\n" );
        
            for ( Enumeration e = _events.elements(); e.hasMoreElements(); )
            {
                CalendarEvent evt = (CalendarEvent) e.nextElement();
                buf.append( evt ).append( "\n" );
            }

            buf.append( "</calendarDate>" );
        }
        else 
        {
            buf.append( " />" );
        }

        return buf.toString();
    }
}
